<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>WebSocket</title>
  </head>
  <body>

    <h1 style="text-align:center;"> SpringBoot - WebSocket 测试</h1>
    <hr>

    <div id="msg"></div>

  </body>


  <script type="text/javascript">
    var websocket = null;
    var userId = 0;

    //用户输入
    userId = prompt("请输入一个用户id");

    //#################  判断浏览器是否支持 Websocket  ###################
    if ('WebSocket' in window) {
      websocket = new WebSocket("ws://localhost:2003/websocket/12345/" + userId);
    } else {
      alert("您的浏览器不支持websocket");
    }
    //#################################################################


    //处理服务器推送过来的信息
    websocket.onmessage = function (event){
      document.getElementById('msg').innerHTML += "<br/>" + event.data;
    }
  </script>
</html>